//
//  TradeOrderViewController.swift
//  Exchange
//
//  Created by rui on 2018/4/25.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

enum TradeOrderType: Int8 {
    case exchange = 0
    case history = 1
}

class TradeOrderViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {

    let kTradeOrderStyleTwoCell = "TradeOrderStyleTwoCell"
    
    var orderType: TradeOrderType {
        return .exchange
    }
    
    lazy var emptyView: EmptyView = {
        let empty = EmptyView()
        empty.message = kText("暂无相关数据")
        return empty
    }()
    
    lazy var viewModel: TradeOrderListViewModel =  {
        let vm = TradeOrderListViewModel()
        vm.response = {[weak self](state,isSuccess,errorMsg)in
            if state == ResponseState.trade_order_list  {
                if isSuccess {
                    self?.tableView.reloadData()
                }else {
                    if let msg = errorMsg {
                        CYMBShow.showMessage(msg)
                    }else {
                        CYMBShow.showMessage(kText("当前无网络,请检查网络"))
                    }
                }
                self?.endRefresh()
            }
        }
        return vm
    }()
    
    
    lazy var tableView: UITableView = {
        let table = UITableView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: kSCREEN_HEIGHT - kNavi_HEIGHT), style: .plain)
        table.delegate = self
        table.dataSource = self
        table.register(UINib.init(nibName: kTradeOrderStyleTwoCell, bundle: nil), forCellReuseIdentifier: kTradeOrderStyleTwoCell)
        table.themeBackground = ThemeManager.ThemeColorKeys.background
        table.themeSeparatorColor = ThemeManager.ThemeColorKeys.separator
        table.tableFooterView = UIView()
        return table
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        initData()
    }
    
    func initData() {
        viewModel.status = self.orderType.rawValue
        self.tableView.mj_footer.isHidden = true
        self.tableView.mj_header.beginRefreshing()
    }
    
    func initUI() {
        self.view.addSubview(tableView)
        self.tableView.mj_header = EoeRefreshHeader(refreshingTarget: self, refreshingAction: #selector(loadNewData))
        self.tableView.mj_footer = EoeRefreshFooter(refreshingTarget: self, refreshingAction: #selector(loadMoreData))
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tableView.reloadData()
    }
    
    @objc func loadNewData(){
        emptyView.removeFromSuperview()
        viewModel.loadNewData()
        NotificationCenter.default.post(name: NSNotification.Name.Exchange.chatNeedRefresh2, object: nil)
    }
    
    @objc func loadMoreData(){
        emptyView.removeFromSuperview()
        viewModel.loadMoreData()
    }
    
    func endRefresh() {
        if viewModel.isNoMoreData{
            self.tableView.mj_footer?.endRefreshingWithNoMoreData()
        }else {
            self.tableView.mj_footer?.endRefreshing()
        }
        self.tableView.mj_header?.endRefreshing()
        self.tableView.mj_footer?.isHidden = viewModel.dataArray.isEmpty
        if viewModel.dataArray.isEmpty {
            emptyView.frame = self.tableView.bounds
            self.tableView.addSubview(emptyView)
        }
    }
    
    
    // MARK: - UITableViewDelegate UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        weak var weakSelf = self
        let model = self.viewModel.dataArray[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: kTradeOrderStyleTwoCell, for: indexPath) as! TradeOrderStyleTwoCell
        cell.model = model
        cell.iconC = {(userId) in
            if userId != AppInfo.default.userId {
                weakSelf?.gotoUserCenter(userId: userId)
            }
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 75
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        let model = self.viewModel.dataArray[indexPath.row]
        if let orderId = model.id {
            let vc = ExchangeChatViewController()
            vc.orderId = orderId
            self.navigationController?.pushViewController(vc, animated: true)
        }else {
            CYMBShow.showMessage(kText("订单不存在"))
        }
        model.isNewMessage = false
    }
    
    // MARK: - event
    func gotoUserCenter(userId: Int) {
        dPrint("用户中心")
        let vc = TradeOrderUserCenterViewController()
        vc.viewModel.userId = userId
        self.navigationController?.pushViewController(vc, animated: true)
    }

}
class HistoryTradeOrderViewController: TradeOrderViewController {
   
    override var orderType: TradeOrderType {
        return .history
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        dPrint("HistoryTradeOrderViewController")
    }
}


